﻿App.Router.map(function () {
    this.resource('users', function () {
        this.route('index', { path: '/index/:page' });
        this.route('detail', { path: '/detail/:user_id' });
        this.route('search', { path: '/search/:where' });
    });

});

App.Router.reopen({
    //指定路由解析类型
    location: 'hash'
});

App.LoadingRoute = Ember.Route.extend({
    
});

App.ApplicationRoute = Ember.Route.extend({

});

App.IndexRoute = Ember.Route.extend({
    redirect: function () {
        this.transitionTo('users.index', "1");
    }
});

/*-- Users --*/
App.User.FIXTURES = [];
App.UsersIndexRoute = Ember.Route.extend({
    model: function (params) {
        var url = '/Model/User?';
        $.ajax(url, {
            type: 'GET',
            async: false,
            data: params
        }).then(function (data) {
            $.each(data, function (i, d) {
                App.User.FIXTURES.push(d);
            });
        });

        return this.store.findAll('user');
    }
});

App.UsersDetailRoute = Ember.Route.extend({
    model: function (params) {
        var url = '/Model/User/' + params.user_id;
        $.ajax(url, {
            type: 'GET',
            async: false
        }).then(function (data) {
            $.each(data, function (i, d) {
                App.User.FIXTURES.push(d);
            });
        });
        return this.store.find('user', params.user_id);
    },

    serialize: function (user) {
        return { user_id: user.get('id') };
    }
});

App.UsersSearchRoute = Ember.Route.extend({
    model: function (params) {
        var wheres = $.parseParam(params.where);
        return this.store.filter('user', function (user) {
            var flag = true;
            $.each(wheres, function (n, d) {
                if (d !== '') {
                    flag = flag && (user.get(n) && user.get(n).toLowerCase().indexOf(d.toLowerCase()) >= 0);
                }
                return flag;
            });
            return flag;
        });
    },
    renderTemplate: function (controller) {
        var usersController = this.controllerFor('users');
        this.render('users/index', { controller: usersController });
    }
});